<?php
/**
 * Created by PhpStorm.
 * User: mr_lee
 * Date: 2017/10/17
 * Time: 下午12:49
 */

namespace app\agenttest\controller;


use app\common\model\Merchant;
use Endroid\QrCode\QrCode;
use think\Controller;
use think\Image;

class Qrcodes extends AgenttestBase
{
    public function qrcode($mid = 602, $url = 'https://wxpay.weilinzhou.com/index.php?s=/Index/pay/id/1.html', $level = 4, $size = 16)
    {
        $map['id'] = $mid;
        $Merchant = new Merchant();
        $merchant = $Merchant->where($map)->field('merchant')->find();
        $qrCode = new QrCode();
        $qrCode->setText($url)
            ->setSize(570)//大小
            //->setLabelFontPath(VENDOR_PATH . 'endroid/qrcode/assets/noto_sans.otf')
            ->setErrorCorrectionLevel('high')
            ->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))
            ->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0));

        header('Content-Type: ' . $qrCode->getContentType());
        $image = $qrCode->writeString();
        $path = $_SERVER['DOCUMENT_ROOT'] . "/qrcode/" . $mid . '.png';
        file_put_contents($path, $image);

        return $path;


    }
    public function sq_qrcode($mid,$url){
        $qrCode = new QrCode();
        $qrCode->setText($url)
            ->setSize(570)//大小
            //->setLabelFontPath(VENDOR_PATH . 'endroid/qrcode/assets/noto_sans.otf')
            ->setErrorCorrectionLevel('high')
            ->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))
            ->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0));

        header('Content-Type: ' . $qrCode->getContentType());
        $image = $qrCode->writeString();
        $path = $_SERVER['DOCUMENT_ROOT'] . "/sqqrcode/" . $mid . '.png';
        file_put_contents($path, $image);
        return "/sqqrcode/" . $mid . '.png';


    }

    //生成二维码
    public function testqrcode(){
        $arr = '5555';
        $res = action('Merchant/qrcodes/cardqrcode',[$arr]);
        if ($res){
            return $res;
        }else{
            $res = '0';
            return $res;
        }
    }

    //卡券二维码
    public function cardqrcode($arr, $level = 4, $size = 16)
    {
        $qrCode = new QrCode();
        $qrCode->setText($arr)
            ->setSize(570)//大小
            //->setLabelFontPath(VENDOR_PATH . 'endroid/qrcode/assets/noto_sans.otf')
            ->setErrorCorrectionLevel('high')
            ->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))
            ->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0));

        header('Content-Type: ' . $qrCode->getContentType());
        $image = $qrCode->writeString();

        $path = $_SERVER['DOCUMENT_ROOT'] . "/qrcode/card/" . $arr . '.png';
        file_put_contents($path, $image);
        $res = request()->domain() . "/qrcode/card/" . $arr . '.png';

        return $res;


    }

    public function mercode($mid = 602, $size = 48, $url = 'https://wxpay.weilinzhou.com/index.php?s=/Index/pay/id/1.html', $merchant = "智慧林州")
    {
        $Merchant=new Merchant();
        $merchant=$Merchant->where(['id'=>$mid])->value('merchant');
        $len = strlen($merchant);
            if ($len >= 6 && $len <= 27) {
                switch ($len) {
                    case 9:
                        $merx = 500;
                        break;
                    case 12:
                        $merx = 470;
                        break;
                    case 15:
                        $merx = 440;
                        break;
                    case 18:
                        $merx = 410;
                        break;
                    case 21:
                        $merx = 380;
                        break;
                    case 24:
                        $merx = 350;
                        break;
                    case 27:
                        $merx = 320;
                        break;

                    default:
                        $merx = 530;
                }
            } elseif ($len > 27) {

                $size = 36;

                switch ($len) {
                    case 28:
                        $merx = 370;
                        break;
                    case 29:
                        $merx = 360;
                        break;
                    case 30:
                        $merx = 360;
                        break;
                    case 31:
                        $merx = 345;
                        break;
                    case 32:
                        $merx = 335;
                        break;
                    case 33:
                        $merx = 330;
                        break;
                    case 34:
                        $merx = 325;
                        break;
                    case 35:
                        $merx = 320;
                        break;


                    default:
                        $merx = 530;
                }

            }
        $qr=$this->qrcode($mid, $url);
        $user_pic = $qr;

        $img = \think\Image::open('./assets/footer.png');

        $qr2 = $_SERVER['DOCUMENT_ROOT'] . "/mercode/" . $mid . '.png';

        $s=$img->water($user_pic,array(300, 420))->text($merchant, VENDOR_PATH . 'endroid/qrcode/assets/noto_sans.otf', $size, '#000000', array($merx, 1020))->save($qr2);
       return $qr2;
    }
}